| @@ -120,7 +120,7 @@ GEM | ||
| 120 | 120 | method_source (0.8.1) | 
| 121 | 121 | mime-types (1.23) | 
| 122 | 122 | mini_portile (0.5.1) | 
| 123 | - multi_json (1.7.8) | |
| 123 | + multi_json (1.7.9) | |
| 124 | 124 | multi_xml (0.5.5) | 
| 125 | 125 | multipart-post (1.2.0) | 
| 126 | 126 | mysql2 (0.3.13) | 
| @@ -166,15 +166,15 @@ GEM | ||
| 166 | 166 | json (~> 1.4) | 
| 167 | 167 | rest-client (1.6.7) | 
| 168 | 168 | mime-types (>= 1.16) | 
| 169 | - rr (1.1.1) | |
| 169 | + rr (1.1.2) | |
| 170 | 170 | rspec (2.14.1) | 
| 171 | 171 | rspec-core (~> 2.14.0) | 
| 172 | 172 | rspec-expectations (~> 2.14.0) | 
| 173 | 173 | rspec-mocks (~> 2.14.0) | 
| 174 | - rspec-core (2.14.3) | |
| 175 | - rspec-expectations (2.14.0) | |
| 174 | + rspec-core (2.14.5) | |
| 175 | + rspec-expectations (2.14.2) | |
| 176 | 176 | diff-lcs (>= 1.1.3, < 2.0) | 
| 177 | - rspec-mocks (2.14.1) | |
| 177 | + rspec-mocks (2.14.3) | |
| 178 | 178 | rspec-rails (2.14.0) | 
| 179 | 179 | actionpack (>= 3.0) | 
| 180 | 180 | activesupport (>= 3.0) | 
| @@ -30,7 +30,9 @@ class Agent < ActiveRecord::Base | ||
| 30 | 30 |  | 
| 31 | 31 | belongs_to :user, :inverse_of => :agents | 
| 32 | 32 | has_many :events, :dependent => :delete_all, :inverse_of => :agent, :order => "events.id desc" | 
| 33 | + has_one :most_recent_event, :inverse_of => :agent, :class_name => "Event", :order => "events.id desc" | |
| 33 | 34 | has_many :logs, :dependent => :delete_all, :inverse_of => :agent, :class_name => "AgentLog", :order => "agent_logs.id desc" | 
| 35 | + has_one :most_recent_log, :inverse_of => :agent, :class_name => "AgentLog", :order => "agent_logs.id desc" | |
| 34 | 36 | has_many :received_events, :through => :sources, :class_name => "Event", :source => :events, :order => "events.id desc" | 
| 35 | 37 | has_many :links_as_source, :dependent => :delete_all, :foreign_key => "source_id", :class_name => "Link", :inverse_of => :source | 
| 36 | 38 | has_many :links_as_receiver, :dependent => :delete_all, :foreign_key => "receiver_id", :class_name => "Link", :inverse_of => :receiver | 
| @@ -72,9 +74,13 @@ class Agent < ActiveRecord::Base | ||
| 72 | 74 | raise "Implement me in your subclass" | 
| 73 | 75 | end | 
| 74 | 76 |  | 
| 75 | - def event_created_within(seconds) | |
| 76 | - last_event = events.first | |
| 77 | - last_event && last_event.created_at > seconds.ago && last_event | |
| 77 | + def event_created_within(days) | |
| 78 | + event = most_recent_event | |
| 79 | + event && event.created_at > days.to_i.days.ago && event.payload.present? && event | |
| 80 | + end | |
| 81 | + | |
| 82 | + def recent_error_logs? | |
| 83 | + most_recent_log.try(:level) == 4 | |
| 78 | 84 | end | 
| 79 | 85 |  | 
| 80 | 86 | def sources_are_owned | 
| @@ -40,7 +40,7 @@ module Agents | ||
| 40 | 40 | end | 
| 41 | 41 |  | 
| 42 | 42 | def working? | 
| 43 | - (event = event_created_within(options[:expected_update_period_in_days].to_i.days)) && event.payload.present? | |
| 43 | + event_created_within(options[:expected_update_period_in_days]) && !recent_error_logs? | |
| 44 | 44 | end | 
| 45 | 45 |  | 
| 46 | 46 | def validate_options | 
| @@ -21,7 +21,7 @@ module Agents | ||
| 21 | 21 | end | 
| 22 | 22 |  | 
| 23 | 23 | def working? | 
| 24 | - last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago | |
| 24 | + last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago && !recent_error_logs? | |
| 25 | 25 | end | 
| 26 | 26 |  | 
| 27 | 27 | def validate_options | 
| @@ -63,7 +63,7 @@ module Agents | ||
| 63 | 63 | end | 
| 64 | 64 |  | 
| 65 | 65 | def working? | 
| 66 | - true | |
| 66 | + !recent_error_logs? | |
| 67 | 67 | end | 
| 68 | 68 |  | 
| 69 | 69 | def value_constructor(value, payload) | 
| @@ -43,7 +43,7 @@ module Agents | ||
| 43 | 43 | end | 
| 44 | 44 |  | 
| 45 | 45 | def working? | 
| 46 | - last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago | |
| 46 | + last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago && !recent_error_logs? | |
| 47 | 47 | end | 
| 48 | 48 |  | 
| 49 | 49 | def receive(incoming_events) | 
| @@ -16,7 +16,7 @@ module Agents | ||
| 16 | 16 | end | 
| 17 | 17 |  | 
| 18 | 18 | def working? | 
| 19 | - last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago | |
| 19 | + last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago && !recent_error_logs? | |
| 20 | 20 | end | 
| 21 | 21 |  | 
| 22 | 22 | def validate_options | 
| @@ -34,7 +34,7 @@ module Agents | ||
| 34 | 34 | end | 
| 35 | 35 |  | 
| 36 | 36 | def working? | 
| 37 | - last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago | |
| 37 | + last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago && !recent_error_logs? | |
| 38 | 38 | end | 
| 39 | 39 |  | 
| 40 | 40 | def receive(incoming_events) | 
| @@ -29,7 +29,7 @@ module Agents | ||
| 29 | 29 | end | 
| 30 | 30 |  | 
| 31 | 31 | def working? | 
| 32 | - last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago | |
| 32 | + last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago && !recent_error_logs? | |
| 33 | 33 | end | 
| 34 | 34 |  | 
| 35 | 35 | def translate(text, to, access_token) | 
| @@ -42,7 +42,7 @@ module Agents | ||
| 42 | 42 | end | 
| 43 | 43 |  | 
| 44 | 44 | def working? | 
| 45 | - last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago | |
| 45 | + last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago && !recent_error_logs? | |
| 46 | 46 | end | 
| 47 | 47 |  | 
| 48 | 48 | def receive(incoming_events) | 
| @@ -58,7 +58,7 @@ module Agents | ||
| 58 | 58 | end | 
| 59 | 59 |  | 
| 60 | 60 | def working? | 
| 61 | - last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago | |
| 61 | + last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago && !recent_error_logs? | |
| 62 | 62 | end | 
| 63 | 63 |  | 
| 64 | 64 | def send_message(message) | 
| @@ -29,7 +29,7 @@ module Agents | ||
| 29 | 29 | end | 
| 30 | 30 |  | 
| 31 | 31 | def working? | 
| 32 | - (event = event_created_within(options[:expected_update_period_in_days].to_i.days)) && event.payload.present? && event.payload[:success] == true | |
| 32 | + (event = event_created_within(options[:expected_update_period_in_days])) && event.payload[:success] == true && !recent_error_logs? | |
| 33 | 33 | end | 
| 34 | 34 |  | 
| 35 | 35 | def default_options | 
| @@ -63,7 +63,7 @@ module Agents | ||
| 63 | 63 | end | 
| 64 | 64 |  | 
| 65 | 65 | def working? | 
| 66 | - (event = event_created_within(options[:expected_update_period_in_days].to_i.days)) && event.payload.present? | |
| 66 | + event_created_within(options[:expected_update_period_in_days]) && !recent_error_logs? | |
| 67 | 67 | end | 
| 68 | 68 |  | 
| 69 | 69 | def default_options | 
| @@ -45,7 +45,7 @@ module Agents | ||
| 45 | 45 | end | 
| 46 | 46 |  | 
| 47 | 47 | def working? | 
| 48 | - (event = event_created_within(options[:expected_update_period_in_days].to_i.days)) && event.payload.present? | |
| 48 | + event_created_within(options[:expected_update_period_in_days]) && !recent_error_logs? | |
| 49 | 49 | end | 
| 50 | 50 |  | 
| 51 | 51 | def default_options | 
| @@ -30,7 +30,7 @@ module Agents | ||
| 30 | 30 | MD | 
| 31 | 31 |  | 
| 32 | 32 | def working? | 
| 33 | - (event = event_created_within(2.days)) && event.payload.present? | |
| 33 | + event_created_within(2) && !recent_error_logs? | |
| 34 | 34 | end | 
| 35 | 35 |  | 
| 36 | 36 | def default_options | 
| @@ -41,7 +41,7 @@ module Agents | ||
| 41 | 41 | default_schedule "8pm" | 
| 42 | 42 |  | 
| 43 | 43 | def working? | 
| 44 | - (event = event_created_within(2.days)) && event.payload.present? | |
| 44 | + event_created_within(2) && !recent_error_logs? | |
| 45 | 45 | end | 
| 46 | 46 |  | 
| 47 | 47 | def wunderground | 
| @@ -44,7 +44,7 @@ module Agents | ||
| 44 | 44 | UNIQUENESS_LOOK_BACK = 30 | 
| 45 | 45 |  | 
| 46 | 46 | def working? | 
| 47 | - (event = event_created_within(options[:expected_update_period_in_days].to_i.days)) && event.payload.present? | |
| 47 | + event_created_within(options[:expected_update_period_in_days]) && !recent_error_logs? | |
| 48 | 48 | end | 
| 49 | 49 |  | 
| 50 | 50 | def default_options | 
| @@ -28,7 +28,7 @@ module Agents | ||
| 28 | 28 | end | 
| 29 | 29 |  | 
| 30 | 30 | def working? | 
| 31 | - (event = event_created_within(options[:expected_update_period_in_days].to_i.days)) && event.payload.present? && event.payload[:success] == true | |
| 31 | + (event = event_created_within(options[:expected_update_period_in_days])) && event.payload[:success] == true && !recent_error_logs? | |
| 32 | 32 | end | 
| 33 | 33 |  | 
| 34 | 34 | def default_options | 
| @@ -78,7 +78,7 @@ module Agents | ||
| 78 | 78 | end | 
| 79 | 79 |  | 
| 80 | 80 | def working? | 
| 81 | - (event = event_created_within(options[:expected_update_period_in_days].to_i.days)) && event.payload.present? | |
| 81 | + event_created_within(options[:expected_update_period_in_days]) && !recent_error_logs? | |
| 82 | 82 | end | 
| 83 | 83 |  | 
| 84 | 84 | def default_options | 
| @@ -1,15 +0,0 @@ | ||
| 1 | -require 'spec_helper' | |
| 2 | - | |
| 3 | -# Specs in this file have access to a helper object that includes | |
| 4 | -# the AgentLogsHelper. For example: | |
| 5 | -# | |
| 6 | -# describe AgentLogsHelper do | |
| 7 | -# describe "string concat" do | |
| 8 | -# it "concats two strings with spaces" do | |
| 9 | -#       expect(helper.concat_strings("this","that")).to eq("this that") | |
| 10 | -# end | |
| 11 | -# end | |
| 12 | -# end | |
| 13 | -describe LogsHelper do | |
| 14 | -  pending "add some examples to (or delete) #{__FILE__}" | |
| 15 | -end | 
| @@ -29,7 +29,7 @@ describe Agents::AdiosoAgent do | ||
| 29 | 29 | it "checks if its generating events as scheduled" do | 
| 30 | 30 | @checker.should_not be_working | 
| 31 | 31 | @checker.check | 
| 32 | - @checker.should be_working | |
| 32 | + @checker.reload.should be_working | |
| 33 | 33 | three_days_from_now = 3.days.from_now | 
| 34 | 34 |  			stub(Time).now { three_days_from_now } | 
| 35 | 35 | @checker.should_not be_working | 
| @@ -42,6 +42,24 @@ describe Agents::WebsiteAgent do | ||
| 42 | 42 | end | 
| 43 | 43 | end | 
| 44 | 44 |  | 
| 45 | + describe '#working?' do | |
| 46 | + it 'checks if events have been received within the expected receive period' do | |
| 47 | + @checker.should_not be_working # No events created | |
| 48 | + @checker.check | |
| 49 | + @checker.reload.should be_working # Just created events | |
| 50 | + | |
| 51 | + @checker.error "oh no!" | |
| 52 | + @checker.reload.should_not be_working # The most recent log is an error | |
| 53 | + | |
| 54 | + @checker.log "ok now" | |
| 55 | + @checker.reload.should be_working # The most recent log is no longer an error | |
| 56 | + | |
| 57 | + two_days_from_now = 2.days.from_now | |
| 58 | +      stub(Time).now { two_days_from_now } | |
| 59 | + @checker.reload.should_not be_working # Two days have passed without a new event having been created | |
| 60 | + end | |
| 61 | + end | |
| 62 | + | |
| 45 | 63 | describe "parsing" do | 
| 46 | 64 | it "parses CSS" do | 
| 47 | 65 | @checker.check |